package com.quanyan.operative.web;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.operative.model.resp.*;
import com.quanyan.operative.service.SnatchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 鸟蛋夺宝controller
 * <p>
 * Created by huadongyang on 2016/12/30.
 */
@RestController
@RequestMapping(value = "/api/operative/snatch", method = RequestMethod.POST, produces = "application/json")
public class SnatchController {
    private static final Logger logger = LoggerFactory.getLogger(SnatchController.class);

    @Autowired
    private SnatchService snatchService;

    /**
     * 获取奖品列表
     *
     * @return
     */
    @RequestMapping(value = "/getSnatchGoodsList")
    APIResponse getSnatchGoodsList(@RequestBody Map map, BindingResult bindingResult, HttpServletRequest request) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer type = (Integer) map.get("type");
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");

        if (type == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        if (pageNum == null || pageNum < 0 || pageSize == null || pageSize <= 0) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        return snatchService.getSnatchGoodsList(type.byteValue(), pageNum, pageSize);
    }


    @RequestMapping(value = "getCodes")
    public APIResponse<RespPayCodes> getCodes(@RequestBody Map map, HttpServletRequest request) {
        Integer uid = (Integer)request.getAttribute("uid");
        if(uid == null){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        Integer targetUID = (Integer)map.get("uid");

        Integer activityID = (Integer)map.get("activityID");
        Integer issueID = (Integer)map.get("issueID");
        Integer recordID = (Integer)map.get("recordID");
        if(activityID == null || issueID == null){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        int actualUID = targetUID != null && targetUID > 0 ? targetUID : uid;

        return snatchService.getCodes(actualUID, activityID, issueID, recordID == null ? 0 : recordID);
    }

    @RequestMapping(value = "eggsInfo")
    public APIResponse<Long> getEggsInfo(@RequestBody Map map, HttpServletRequest request) {
        Integer uid = (Integer)request.getAttribute("uid");
        if(uid == null){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return snatchService.getEggsInfo(uid);
    }

    @RequestMapping(value = "doAnte")
    public APIResponse<RespPayCodes> doPay(@RequestBody Map map, HttpServletRequest request) {
        Integer uid = (Integer)request.getAttribute("uid");
        if(uid == null){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        Integer activityID = (Integer)map.get("activityID");
        Integer issueID = (Integer)map.get("issueID");
        Integer anteNum = (Integer)map.get("anteNum");
        if(activityID == null || issueID == null || anteNum == null){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        return snatchService.doAnte(uid, activityID, issueID, anteNum);
    }

    @RequestMapping(value = "enterSpec")
    public APIResponse<RespEnterSpec> enterSpec(@RequestBody Map map, HttpServletRequest request) {
        //带随机数，防止请求缓存
        Integer uid = (Integer)request.getAttribute("uid");

        Integer activityID = (Integer)map.get("activityID");
        Integer issueID = (Integer)map.get("issueID");
        if(activityID == null || issueID == null){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        return snatchService.enterSpec(uid == null ? 0 : uid, activityID, issueID);
    }

    @RequestMapping(value = "getParticipants")
    public APIResponse<RespSnatchParticipantsInfo> getParticipants(@RequestBody Map map, HttpServletRequest request) {
        Integer uid = (Integer)request.getAttribute("uid");
        if(uid == null){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        Integer activityID = (Integer)map.get("activityID");
        Integer issueID = (Integer)map.get("issueID");
        Integer pageNo = (Integer)map.get("pageNo");
        Integer perPage = (Integer)map.get("perPage");
        if(activityID == null || issueID == null){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        return snatchService.getParticipants(activityID, issueID, pageNo == null ? 0 : pageNo, perPage == null ? 10 : perPage);
    }


    @RequestMapping(value="snatchRecord")
    public APIResponse<RespSnatchRecords> snatchRecord(@RequestBody Map map, HttpServletRequest request){
        Integer uid = (Integer)request.getAttribute("uid");
        if(uid == null){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        Integer issueID = (Integer)map.get("issueID");
        Integer pageNo = (Integer)map.get("pageNo");
        Integer perPage = (Integer)map.get("perPage");

        return snatchService.snatchRecord(uid, issueID == null ? 0 : issueID, pageNo == null ? 0 : pageNo, perPage == null ? 10 : perPage);
    }

    @RequestMapping(value="mine")
    public APIResponse<RespSnatchMine> mine(@RequestBody Map map, HttpServletRequest request){
        Integer uid = (Integer)request.getAttribute("uid");
        if(uid == null){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        Integer type = (Integer)map.get("type");
        Integer pageNo = (Integer)map.get("pageNo");
        Integer perPage = (Integer)map.get("perPage");

        return snatchService.mine(uid, type == null ? 0 : type, pageNo == null ? 0 : pageNo, perPage == null ? 1000 : perPage);
    }


    @RequestMapping(value="myWinnings")
    public APIResponse<List<RespSnatchWinningInfo>> myWinnings(@RequestBody Map map, HttpServletRequest request){
        Integer uid = (Integer)request.getAttribute("uid");
        if(uid == null){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        Integer pageNo = (Integer)map.get("pageNo");
        Integer perPage = (Integer)map.get("perPage");

        return snatchService.myWinnings(uid, pageNo == null ? 0 : pageNo, perPage == null ? 10 : perPage);
    }

    @RequestMapping(value = "getWinningInfoList")
    public APIResponse getWinningInfoList(@RequestBody Map map, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");

        if (pageNum == null || pageNum < 0 || pageSize == null || pageSize <= 0) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        return snatchService.getWinningInfoList(pageNum,pageSize);
    }
}
